package com.framework.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Image;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Textbox;

public class MapeadorDeComponentesUtil {
	
	private static Logger log = Logger
			.getLogger(MapeadorDeComponentesUtil.class);
	
	@Retention(RetentionPolicy.RUNTIME)
	@Target(ElementType.FIELD)
	public @interface Componente {
		String nombre() default "$";
	}
   
	public static Map<String, Object> getMapa(Component component){ 
		Map<String, Object> map = new HashMap<String, Object>();
		Collection<Component> components = component.getFellows();
		for (Component componentTemp : components) {
			agregarAlMapa(componentTemp, map);
		} 
		return map;
	}
 
	public static void agregarAlMapa(Component componentTemp,
			Map<String, Object> map) {
		if(componentTemp instanceof Textbox){
			 Textbox textbox = (Textbox) componentTemp;
			 String value  = textbox.getValue();
			 map.put("" + textbox.getId(), "" + value);  
		 }else if(componentTemp instanceof Datebox){
			 Datebox datebox = (Datebox) componentTemp;
			 if(datebox.getValue()!=null){
				 map.put("" + datebox.getId(), "" + datebox.getText()); 
			 }
		 }else if(componentTemp instanceof Listbox){
			Listbox listbox = (Listbox) componentTemp;
			if (listbox.getSelectedItem() != null
					&& !listbox.getSelectedItem().getValue().toString().trim()
							.isEmpty()) {
				String value = listbox.getSelectedItem().getLabel().toString();
				if(!value.contains("-seleccione-")){
					map.put("" + listbox.getId(), "" + value);					
				}
			} else {
				map.put("" + listbox.getId(), "");
			}
		 }else if(componentTemp instanceof Radiogroup){
			 Radiogroup radio = (Radiogroup) componentTemp;
				 if(radio.getSelectedItem() != null){
					 String value  = radio.getSelectedItem().getValue().toString();
					 map.put("" + radio.getId(), "" + value); 
				 }else{
					 map.put("" + radio.getId(), ""); 
				 }
		 }
//		 else if(componentTemp instanceof Radio){
////			 Radio textbox = (Radio) componentTemp;
////			 
//		 }
		 else if(componentTemp instanceof Checkbox){
			 Checkbox textbox = (Checkbox) componentTemp;
			 boolean value  = textbox.isChecked();
			 map.put("" + textbox.getId(), value); 
		 }else if(componentTemp instanceof Bandbox){
			 Bandbox textbox = (Bandbox) componentTemp;
			 String value  = textbox.getValue() != null ? textbox.getValue().replace("null", "") : "";  
			 map.put("" + textbox.getId(), "" + value); 
		 }else if(componentTemp instanceof Label){
			 Label textbox = (Label) componentTemp;
			 String value  = textbox.getValue();
			 map.put("" + textbox.getId(), "" + value); 
		 }else if(componentTemp instanceof Intbox){
			 Intbox textbox = (Intbox) componentTemp;
			 Integer value  = textbox.getValue();
			 if(value != null && value.intValue() > 0){
				 map.put("" + textbox.getId(), textbox.getText());				 
			 }
		 }else if(componentTemp instanceof Doublebox){
			 Doublebox textbox = (Doublebox) componentTemp;
			 Double value  = textbox.getValue();
			 if(value != null && value.doubleValue() > 0){
				 map.put("" + textbox.getId(), textbox.getText());				 
			 }
		 }else if(componentTemp instanceof Image){
			 try {
				Image image = (Image) componentTemp;
				if(image.getContent() != null){
					InputStream inputStream = new ByteArrayInputStream(image.getContent().getByteData());
					 map.put("" + image.getId(), inputStream);
				}else if(image.getSrc() != null && !image.getSrc().trim().isEmpty()){
					ServletContext context =  ((HttpServletRequest) Executions
							.getCurrent().getNativeRequest()).getSession().getServletContext();
					String SUBREPORT_DIR = context.getRealPath(image.getSrc());
					File file = new File(SUBREPORT_DIR);
					if(file.exists()){
						map.put("" + image.getId(), new FileInputStream(file)); 
						//log.info("Imagen agregada");  
					}
				}
			} catch (Exception e) {
				log.error(e.getMessage(), e); 
			}
		 }
	}
	
	public static String getValorListbox(Listbox listbox){
		Listitem listitem = listbox.getSelectedItem();
		if(listitem != null && !listitem.getValue().toString().trim().isEmpty()){
			return listitem.getLabel().toString();
		}else{
			return "";
		}
	}
	
}
